import os
from lxml import etree
import click

from commonroad.scenario.scenario import Tag
from commonroad.common.file_writer import CommonRoadFileWriter, OverwriteExistingFile
from commonroad.planning.planning_problem import PlanningProblemSet
from commonroad.common.file_reader import CommonRoadFileReader
from crdesigner.map_conversion.map_conversion_interface import osm_to_commonroad

@click.command(name='osm_to_cr')
@click.option('--osm_path', help='osm 地图文件路径')
@click.option('--out_cr_path', help='输出的 commonroad cr.xml 文件路径')
def osm_to_cr(osm_path:str, out_cr_path:str) -> None:
    '''
    将 osm 格式的地图文件转换 commonroad 格式的 cr.xml
    '''
    # 加载 OSM 文件
    scenario = osm_to_commonroad(osm_path)

    # 保存 cr 格式的场景文件
    writer = CommonRoadFileWriter(
        scenario=scenario,
        planning_problem_set=PlanningProblemSet(),
        author='zhaoshucheng',
        affiliation='Tongji University',
        source='CommonRoad Scenario Designer',
        tags={Tag.URBAN},
    )
    writer.write_to_file(out_cr_path)

@click.command(name='osm_to_sumo')
@click.option('--osm_path', help='osm 地图文件路径')
@click.option('--out_sumo_path', help='输出的 commonroad net.xml 文件路径')
def osm_to_sumo(osm_path:str, out_sumo_path:str) -> None:
    '''
    将 osm 格式的地图文件转换为 sumo 格式
    '''
    cmd = 'netconvert --osm-files {0} --output-file {1}'.format(osm_path, out_sumo_path)
    print(cmd)
    os.system(cmd)

@click.group()
def cli():
    pass

if __name__ == '__main__':
    cli.add_command(osm_to_cr)
    cli.add_command(osm_to_sumo)
    cli()